﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using System.Reflection;
using System.Data;
using System.Collections;
using System.Text;

using System.Text.RegularExpressions;
using System.IO;
using System.Threading;
using ZD_Utility;
using ZD_Utility.ZD_OpenAPI;
using System.Net;
using System.IO;
namespace ZD_Console
{
    class Program
    {

        static Random rd = new Random();
        static void Main(string[] args)
        {

            // geturl();
            downimg();
        }

        public static void downimg()
        {
            var a = File.ReadAllLines("D:\\img.txt");
            string filepath = "D:\\SexImg\\";
            WebClient mywebclient = new WebClient();
            foreach (var item in a)
            {
                try
                {
                    string extension = Path.GetExtension(item);
                    WebRequest request = WebRequest.Create(item);
                    WebResponse response = request.GetResponse();
                    Stream reader = response.GetResponseStream();
                    string path = filepath + DateTime.Now.ToShortDateString() + rd.Next().ToString() + extension;
                    FileStream writer = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write);
                    byte[] buff = new byte[3 * 1024];
                    int c = 0; //实际读取的字节数
                    while ((c = reader.Read(buff, 0, buff.Length)) > 0)
                    {
                        writer.Write(buff, 0, c);
                    }
                    writer.Close();
                    writer.Dispose();
                    reader.Close();
                    reader.Dispose();
                    response.Close();
                }
                catch (Exception)
                {
                    continue;
                }

            }
        }


        public static void geturl()
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 1; i < 10; i++)
            {
                HttpGet a = getRequest("http://www.maxmaya.com/forumdisplay.php?fid=5&page=" + i.ToString() + "");

                string str = a.SendRequest();

                str = str.Substring(str.IndexOf(@"<div class=""spaceborder"" style=""width: 96%; border-top: none;border-bottom: none"">"));
                str = str.Substring(0, str.IndexOf("<div class=\"maintable\">"));
                Regex reg = new Regex("(?is)<a href=\"(?<href>viewthread.php?[^>]*?extra.*?)\">.*?</a>");
                Regex reg1 = new Regex("(?is)<img.*?src=\"(?<src>[^>]*?)\".*?>");
                var Matches = reg.Matches(str);
                HttpGet b = null;
                foreach (Match item in Matches)
                {
                    b = getRequest("http://www.maxmaya.com/" + item.Groups["href"].Value);
                    string str1 = b.SendRequest();
                    str1 = str1.Substring(str1.IndexOf(@"class=""t_msgfont"""));
                    str1 = str1.Substring(0, str1.IndexOf("</div"));
                    var imgmatches = reg1.Matches(str1);
                    foreach (Match item1 in imgmatches)
                    {
                        sb.AppendLine(item1.Groups["src"].Value);
                    }

                }
                using (StreamWriter sw = new System.IO.StreamWriter("D:\\img.txt", true))
                {
                    sw.Write(sb.ToString());
                }
            }

            Console.WriteLine("success");

            Console.ReadLine();
        }

        public static HttpGet getRequest(string uri)
        {

            HttpGet a = new HttpGet(uri);
            a.req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
            a.req.UserAgent = "Mozilla/5.0 (Windows NT 5.1; rv:12.0) Gecko/20100101 Firefox/12.0";
            a.req.Referer = "http://www.maxmaya.com/forumdisplay.php?fid=5";
            //a.req.Connection = "keep-alive";
            a.req.KeepAlive = true;
            a.req.Host = "www.maxmaya.com";

            CookieCollection aa = new CookieCollection();

            aa.Add(new Cookie("cdb_auth", "P8oAuy5oV5Tg%2FhXNOGfdNDKNzRR9HIaIkxFnDfd77%2FrXZ%2FPB7R9mvuIlm3qflkd7CA", "/", "www.maxmaya.com"));
            aa.Add(new Cookie("cdb_cookietime", "2592000", "/", "www.maxmaya.com"));
            aa.Add(new Cookie("cdb_oldtopics", "D1126805D1126807D1126779D1126809D1126793D1126795D1126811D1124560D1125602D1125603D1126816D", "/", "www.maxmaya.com"));
            aa.Add(new Cookie("cdb_sid", "OOrzdb", "/", "www.maxmaya.com"));
            aa.Add(new Cookie("cdb_visitedfid", "5", "/", "www.maxmaya.com"));
            aa.Add(new Cookie("is_use_cookied", "yes", "/", "www.maxmaya.com"));
            aa.Add(new Cookie("is_use_cookiex", "yes", "/", "www.maxmaya.com"));
            CookieContainer container = new CookieContainer();
            container.Add(aa);
            a.req.CookieContainer = container;
            return a;
        }
    }
}
